// pages/gouwuche/gouwuche.js
const app=getApp();
var util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgurl: app.data.imgurl,
        allcheck: 1,
        p:0,//当前页
        lists: [],
		nums:0,//总金额
		userid:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		
        var userid=app.globalData.userId;
		
        this.setData({
			userid:userid
        })
    },
  	//获取订单列表
	cartlist: function () {
		wx.showLoading({
			title: '数据加载中...',
		})
        var that = this;
        var userid = this.data.userid;
        util.httpRrquest(app.data.rooturl + 'mall/cartList', 'GET', { 'userid': userid })
        .then(function (response) {
            var data = response.data.data;
            wx.hideLoading();
            that.setData({
                lists: data
            })
        });
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
		var that = this;
		that.setData({
			lists: [],
			allcheck:1,
			nums:0
		})
		this.cartlist();//获取购物车列表
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
     
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
     
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
		var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    },
	checkChange(e) {
        var that=this;
		var index = e.currentTarget.dataset.index
		console.log(index);
		var item = this.data.lists[index]
		if(item.check==1){
			item.check=2;
		}else{
			item.check=1;
		}
        if(item.check==2){
            this.data.nums = (parseFloat(this.data.nums) + parseFloat(item.price*item.goodNum)).toFixed(2);
        }else{
            this.data.nums = (parseFloat(this.data.nums) - parseFloat(item.price * item.goodNum)).toFixed(2);
        }
		var listss = "lists[" + index + "]";
        this.setData({
            [listss] : item,
            nums:this.data.nums
        })

		//是否达到全选 或 选 取消
		var num = 0;
		var num1 = 0;
		var lists=that.data.lists
		for (var x in lists) {
			var ist = that.data.lists[x].check
			if (ist==2) {
				num = num + 1;
			} else {
				num1 = num1 + 1;
			}
		}
		if(that.data.lists.length==num){
            this.setData({
                allcheck:2
            })
        }else{
            this.setData({
                allcheck: 1
            })
		}
    },
    allcheckChange() {
		var that = this;  
        var allcheck = this.data.allcheck
		if(allcheck==1){
			this.setData({
				allcheck: 2
			})
		}else{
			this.setData({
				allcheck: 1
			})
		}
        var lists=that.data.lists;
        that.data.nums=0;
        for(var x in lists){
			that.data.lists[x].check = that.data.allcheck;
			if(that.data.allcheck==2){
				that.data.nums = (parseFloat(that.data.nums) + parseFloat(that.data.lists[x].goodNum * that.data.lists[x].price)).toFixed(2)
			//console.log(that.data.nums);
			}else{
				that.data.nums=0;
			}
        }
		that.setData({
			lists: that.data.lists,
			nums:that.data.nums
		})
    },
    jiajian(e) {
        var that=this;
        var index = e.currentTarget.dataset.index
        var state = e.currentTarget.dataset.state
        var item = this.data.lists[index]
        var id=item.id;//购物信息id
        var price=item.price;//价格
        var intem=item.goodNum-1;
        if (state == "-1" && item.goodNum > 1) {
			  item.goodNum--
			  var num=item.amount-item.price;
			  item.amount= num.toFixed(2);
        }
      	if (state == "+1" && item.goodNum < 99) {
			item.goodNum++
			var num=parseFloat(item.amount)+parseFloat(item.price);
			item.amount= num.toFixed(2);
		}
		
        var listss = "lists[" + index + "]";
		var lists = that.data.lists;
		var nums = that.data.nums;
		for (var x in lists) {
			var id_ = lists[x].id;
			if (lists[x].check==2) {
				if (id_ == id) {
					console.log(id);
					if (state == '-1') {
						if (intem>=1) {
							var nums = that.data.nums - lists[x].price;
							nums = nums.toFixed(2);
								that.setData({
									nums:nums
								})
						}
					} else {
						var nums = parseFloat(that.data.nums) + parseFloat(lists[x].price);
						console.log(nums);
						nums = nums.toFixed(2);
						that.setData({
							nums: nums
						})
					}
				}
			}
		}
		if (state == "-1" && intem<=0){

		}else{
            if(state == "+1"){
                state = 1;
            }
			wx.request({
                url: app.data.rooturl + "mall/cartNum",
                method:'GET',
				data: { 'userid':this.data.userid,'id': id, 'num': state },
				success: function (res) {
                    state = "+1";
				}
			});
		}
		console.log(item);
        this.setData({
            [listss]: item,
            nums:that.data.nums
        })
    },
	delcart(){
		var str='';
		var that=this;
		var lists = that.data.lists;
		var lists_=[];
		for (var x in lists) {
			var id = lists[x].id;
			if (lists[x].check==2) {
				that.data.nums = that.data.nums - lists[x].goodNum * lists[x].price
				if(str==''){
					str=id;
				} else{
					str=str+','+id;
				}
			}else{
				lists_.push(lists[x]);
			}
		}
		if(str==''){
			wx.showToast({ title: '请选择要删除的数据', icon: 'none', duration: 1000 })
			return;
		}
		if(lists_.length==0){
			this.setData({
				allcheck:1
			})
		}
		this.setData({
			lists: lists_,
			nums:that.data.nums
		})
		//调用方法删除购物车信息
		wx.request({
            url: app.data.rooturl + "mall/delCart",
            method:'GET',
			data: { 'ids': str,'userid':this.data.userid},
			success: function (res) {

			}
		});

	},
  	//结算方法 跳转订单页面
  	jiesuan:function(e){
		var userid=this.data.userid;
		var str = '';
		var that = this;
		var lists = that.data.lists;
		for (var x in lists) {
			var id = lists[x].id;
			if (lists[x].check==2) {
				if (str == '') {
					str = id;
				} else {
					str = str + ',' + id;
				}
			}
		}
		if (str == '') {
			wx.showToast({ title: '请选择商品', icon: 'none', duration: 1000 })
			return;
		}
		
		
		wx.navigateTo({
			url: '/pages/dingdanqueren/dingdanqueren?idstr=' + str
		})
	}
})